import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbjob } from '../../../../atoms/db';

const logger = anylogger('pages/api/zllweapp/zuoyepiyue/s004.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

export type Message = {
	selectedclasses: string;
	teacherscomments: string;
	id: string
}

/**
 * pages/api/zllweapp/zuoyepiyue/s004.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { selectedclasses, teacherscomments, id } = req.body as Message;

		if (!selectedclasses) {
			res.status(200).json({ ok: false, message: '请选择评分' }); return;
		}
		if (!teacherscomments) {
			res.status(200).json({ ok: false, message: '请输入评语' }); return;
		}
		if (!id) {
			res.status(200).json({ ok: false, message: '页面参数错误' }); return;
		}
		await tbjob().update({
			teacherscomments: teacherscomments,
			mark: selectedclasses,
			approvedlogo: 1
		}).where({
			sjob_id: id
		});

		res.status(200).json({ ok: true });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
